package aufgabe2_pop3;

import java.util.LinkedList;
import java.util.List;


public class Server_Administration extends Thread{

	 String pass = "root";
	
	 Server_Administration_Control_I_for_Server_Administration control;

	 static List<Server> server_list = new LinkedList<Server>();
	
	 Datenbank datenbank;
	
	public Server_Administration(Datenbank datenbank) {
		control = new Server_Administration_Control(1, "root");
		this.datenbank = datenbank;
	}
	
	public void run(){
		datenbank.log("Server Administration", "Server", "Erzeuge");		
		Server new_server = new Server(11000, (Server_Administration_Control_I_for_Server)control, datenbank);
		
		datenbank.log("Server Administration","Server","Hinzu Fuegen ");
		Server_Administration.server_list.add(new_server);
		
		datenbank.log("Server Administration","Server","Starten"); 
		new_server.start();
		
		datenbank.log("Server Administration","Server"," Lauft");
		
		datenbank.log("Server Administration", "Server Administration", "Run: " + control.server_run());
		
		while(control.server_run()){
			my_sleep();
		}
		
		datenbank.log("Server Administration", "Server Administration", "Beende Server");
		while( ! Server_Administration.server_list.isEmpty() ){
			Server_Administration.server_list.get(0).stop_running();
			Server_Administration.server_list.remove(0);
		}
		
		datenbank.log("Server Administration", "Server Administration", "Warte auf Clients");
		while(control.server_threads_running()){
			my_sleep();
		}
		datenbank.log("Server Administration", "Server Administration", "Beende");
	}
	
	public void my_sleep(){
		try {
			sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
